package org.example.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.tms.config.AjaxResult;
import org.example.tms.mapper.DeptMapper;
import org.example.tms.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptMapper deptMapper;

    @GetMapping("/depts")
    public AjaxResult deptList() {
        List<Dept> depts = deptMapper.selectList(null);
        return AjaxResult.success(depts);
    }
    @DeleteMapping("/depts")
    public AjaxResult deleteDept(Integer id) {
        int i = deptMapper.deleteById(id);
        return AjaxResult.success(i);
    }
    @PostMapping("/depts")
    public AjaxResult insertDept(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        int i = deptMapper.insert(dept);
        return AjaxResult.success(i);
    }
    @GetMapping("/depts/{id}")
    public AjaxResult selectDept(@PathVariable Integer id) {
        Dept dept = deptMapper.selectById(id);
        return AjaxResult.success(dept);
    }
    @PutMapping("/depts")
    public AjaxResult updateDept(@RequestBody Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dept.getId());
        int i = deptMapper.updateById(dept);
        return AjaxResult.success(i);
    }

}
